package com.xish.view;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xish.model.bean.WzmActivity;
import com.xish.model.bean.WzmPoi;
import com.xish.util.qiniu.Image;
import com.xish.util.qiniu.ImageService;
import com.xish.util.qiniu.ImageType;
import com.xish.view.json.Activity;
import com.xish.view.json.Poi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JsonConverter {

    private static final Logger LOG = LoggerFactory.getLogger(JsonConverter.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ImageService imageService;

    public Activity convertWzmActivity(WzmActivity wzmActivity) {
        if (wzmActivity == null) {
            return null;
        }

        Activity activity = new Activity();

        activity.setId(wzmActivity.getId());
        activity.setTitle(wzmActivity.getTitle());
        activity.setTitleVice(wzmActivity.getTitleVice());
        activity.setImage(getActivityImage(wzmActivity.getPic(), 0));

        activity.setDistanceDescription("");
        activity.setIsFollow(0);

        return activity;
    }

    public Poi convertWzmPoi(WzmPoi wzmPoi) {
        if (wzmPoi == null) {
            return null;
        }

        return new Poi(wzmPoi.getTitle());
    }

    private String getImageFromJsonEncodedString(String jsonEncodedString, int index,
                                                 String imageSpec, ImageType imageType, boolean isPublic) {
        LOG.debug("content={}, index={}", jsonEncodedString, index);

        if ((jsonEncodedString == null) || jsonEncodedString.isEmpty() || index < 0) {
            return "";
        }

        try {
            TypeReference<List<String>> typeRef = new TypeReference<List<String>>() {};
            List<String> list = objectMapper.readValue(jsonEncodedString, typeRef);

            if (list.isEmpty() || (index >= list.size())) {
                return "";
            } else {
                return imageService.getImageUrl(
                        new Image(list.get(index), imageSpec, imageType, isPublic));
            }

        } catch (Exception e) {
            LOG.error("fail to json content -> " + jsonEncodedString, e);

            return "";
        }
    }

    public String getActivityImage(String jsonEncodedString, int index) {
        return getImageFromJsonEncodedString(jsonEncodedString, index,
                "!720x500", ImageType.Activity, true);
    }

    public String getShopImage(String jsonEncodedString, int index) {
        return getImageFromJsonEncodedString(jsonEncodedString, index,
                "!136x136", ImageType.Shop, true);
    }

    public String getGenreImage(String image) {
        LOG.debug("content={}", image);
        return imageService.getImageUrl(
                new Image(image, "", ImageType.Other, true));
    }
}
